import { ref, reactive, computed, watch } from 'vue'
import { defineStore } from 'pinia'
import {
  insertFriendRequest,
  insertFriendsRequests,
  insertFriendsRelationShip,
  queryFriendRelation,
  queryFriendRequests,
} from '@/utils/Electron'
import { chatRoomList, allChatRecords, friendsRelationShip, allFriendsRequests, requestMessage } from '@/lib/api'
import { classifyNames } from '@/utils/PinYinUtils'
import useAuthStore from './authStore'




export default defineStore('friends', (store) => {
  const authStore = useAuthStore()
  // 定义属性
  const friendsRelation = ref<Array<any>>([]) // 好友信息
  const friendsRequests = ref<Array<any>>([]) // 好友请求列表
  
  // 对好友名字进行分类
  const friendsRelationComputed = computed(() => {
    return friendsRelation.value.length > 0 ? classifyNames(friendsRelation.value) : []
  })

  // 获取本地好友请求信息
  const getLocalFriendsRequests = () => {
    const userInfo = authStore.userInfo
    queryFriendRequests(userInfo.userId).then((res: any) => {
      // 将聊天数据放入Pinia中
      if (res.data.list || res.data.list.length > 0) {
        setFriendsRequests(res.data.list)
      }
    })
  }

  // 获取服务端好友请求信息
  const getRemoteFriendsRequests = () => {
    return allFriendsRequests().then((res: any) => {
      if (res.code === 0 && res.data.list.length > 0) {
        insertFriendsRequests(res.data.list)
        setFriendsRequests(res.data.list)
      }
    })
  }

  // 获取服务端好友信息
  const getRemoteFriendsRelation = () => {
    return friendsRelationShip().then((res: any) => {
      if (res.code === 0 && res.data.list.length > 0) {
        insertFriendsRelationShip(res.data.list)
        setFriendsRelation(res.data.list)
      }
    })
  }

  // 获取服务端好友信息
  const getLocalFriendsRelation = () => {
    const userInfo = authStore.userInfo
    queryFriendRelation(userInfo.userId).then((res: any) => {
      // 将聊天数据放入Pinia中
      if (res.data.list || res.data.list.length > 0) {
        setFriendsRelation(res.data.list)
      }
    })
  }

  // 设置当前用户的好友列表
  const setFriendsRelation = (list: any) => {
    friendsRelation.value = list.map((item: any) => ({
      id: item.id,
      relationId: item.relationId,
      avatar: item.avatar,
      loginName: item.loginName,
      username: item.userName || item.username
    }))
  }

  const addFriendsRelation = (obj: any) => {
    insertFriendsRelationShip([obj])
    friendsRelation.value.unshift({
      avatar: obj.avatar,
      id: obj.relationId,
      loginName: obj.loginName,
      relationId: obj.id,
      username: obj.userName
    })
  }

  // 设置当前用户的好友请求列表
  const setFriendsRequests = (list: any) => {
    friendsRequests.value = list.map((item: any) => ({
      id: item.id,
      requestId: item.requestId,
      requesterId: item.requesterId,
      receiverId: item.receiverId,
      createTime: item.createTime,
      status: item.status,
      requestMessage: item.requestMessage,
      requestUserName: item.requestUserName,
      requestAvatar: item.requestAvatar,
    }))
  }

  // 增加好友请求
  const addFriendRequest = (request: any) =>  {
    insertFriendRequest(request).then((res)=> {
      friendsRequests.value.unshift({
        id: res,
        requestId: request.id,
        requesterId: request.requesterId,
        receiverId: request.receiverId,
        createTime: request.createTime, //
        status: request.status,
        requestMessage: request.requestMessage,
        requestUserName: request.requestUserName,
        requestAvatar: request.requestAvatar,
      })
    })    
  }

  // 修改状态
  const updateFriendRequestStatus = (id: number, status: number) => {
    friendsRequests.value.forEach((item)=> {
      if(item.id === id) {
        item.status = status
      }
    })
  }

  // 重置状态
  const resetStore = () => {
    setFriendsRelation([])
    setFriendsRequests([])
  }

  return {
    friendsRequests,
    friendsRelation,
    friendsRelationComputed,
    updateFriendRequestStatus, // 修改状态
    addFriendRequest,
    getRemoteFriendsRequests, // 获取远程好友请求
    getRemoteFriendsRelation, // 获取远程好友关系
    getLocalFriendsRequests,
    getLocalFriendsRelation,
    addFriendsRelation,
    resetStore
  }
})
